package a.codetop;

public class _13合并有序数组 {
    private int[] mergeOrderNums(int[] nums1, int[] nums2) {

        int[] res = new int[nums1.length + nums2.length];
        int h1=0,h2=0, n1=nums1.length, n2=nums2.length;

        int i=0;
        while(h1<n1 && h2<n2){
            if(nums1[h1] <= nums2[h2]) res[i++] = nums1[h1++];
            else res[i++] = nums2[h2++];
        }


        if(h1==n1){
            //剩h2
            for (int k=h2;k<n2;k++) res[i++] = nums2[k];
        }
        else if(h2==n2){
            //剩h1
            for (int j=h1;j<n1;j++) res[i++] = nums1[j];
        }

        return res;

    }

    //都放到nums1里版
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int[] res = new int[nums1.length + nums2.length];
        int h1=0,h2=0, n1=nums1.length, n2=nums2.length;

        int i=0;
        while(h1<m && h2<n){
            if(nums1[h1] <= nums2[h2]) res[i++] = nums1[h1++];
            else res[i++] = nums2[h2++];
        }


        if(h1==m){
            //剩h2
            for (int k=h2;k<n;k++) res[i++] = nums2[k];
        }
        else if(h2==n){
            //剩h1
            for (int j=h1;j<m;j++) res[i++] = nums1[j];
        }

        for(int t=0;t<nums1.length;t++) nums1[t]=res[t];
        return;
    }
}
